package com.hudongwx.yuan_sdjj.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.yuan_sdjj.R;
import com.hudongwx.yuan_sdjj.activity.GridBrowserActivity;
import com.hudongwx.yuan_sdjj.activity.HomeDialogActivity;
import com.hudongwx.yuan_sdjj.activity.LabelBrowserActivity;
import com.hudongwx.yuan_sdjj.activity.LoginActivity;
import com.hudongwx.yuan_sdjj.utils.API.API;
import com.hudongwx.yuan_sdjj.utils.JsonParse;
import com.hudongwx.yuan_sdjj.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.http.app.ResponseParser;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_home)
public class HomeFragment extends Fragment {
    private String TAG = "HomeFragment";
    @ViewInject(R.id.home_lv_1)
    private ListView listView;
    private MyAdapter myAdapter;
    @ViewInject(R.id.home_iv_1)
    private ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        RequestParams request = new RequestParams(API.url_home);
        // 获得Json数据并传给adapter
        x.http().get(request, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                //创建adapter， 绑定给listview
                myAdapter = new MyAdapter(result);
                listView.setAdapter(myAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getActivity(), HomeDialogActivity.class), 2);
            }
        });
        return view;
    }

    //跟换性别图标
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode != 2) return;
        switch (data.getIntExtra("sex", 0)) {
            case 0:
                imageView.setImageResource(R.drawable.home_man);
                break;
            case 1:
                imageView.setImageResource(R.drawable.home_woman);
                break;
            case 2:
                imageView.setImageResource(R.drawable.home_all);
                break;
        }
    }

    //收藏的登陆跳转
    @Event(R.id.home_iv_2)
    private void home2Login(View view) {
        startActivityForResult(new Intent(getActivity(), LoginActivity.class), 3);

    }

    class MyAdapter extends BaseAdapter {
        JSONObject allData;

        public MyAdapter(JSONObject jobj) {
            this.allData = jobj;
        }

        public View getFirstList(JSONObject data) {
            Log.i(TAG, "getFirstList: ");
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.list_first_main, null);
            /***********头部改成活动界面************/
            ImageView ivInterim = (ImageView) view.findViewById(R.id.first_interim_iv);
            try {
               x.image().bind(ivInterim,data.getJSONObject("homeStyle").getString("wealthBg"));
//                Log.i(TAG, "getFirstList: "+data.getString("wealthBg"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            /***********五种商品分类************/
            //监听事件
            FirstItemClicListener firstItem = new FirstItemClicListener();
            //设置父布局
            LinearLayout goodsLayout = (LinearLayout) view.findViewById(R.id.first_list_ll_1);

            LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            lParams.weight = 1;

            try {
                JSONArray goodsArray = data.getJSONArray("templates").getJSONObject(0).getJSONArray("item");
                int len = goodsArray.length();
                for (int i = 0; i < len; i++) {
                    JSONObject item = goodsArray.getJSONObject(i);
                    ImageView img = new ImageView(getActivity());
                    img.setLayoutParams(lParams);
                    String imgUrl = item.getString("imgUrl");
                    x.image().bind(img, imgUrl);
                    //保存数据
                    img.setTag(item.getString("link") + "|" + item.getString("name"));
                    //设置点击事件
                    img.setOnClickListener(firstItem);
                    //添加到父容器
                    goodsLayout.addView(img);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            /***********限时活动************/
            FirstItemClicListener2 firstItem2 = new FirstItemClicListener2();
//            ImageView [] image = new ImageView[4];
//            for (int i = 0; i <4 ; i++) {
//                getResources().getIdentifier(R.id)
//            }
            LinearLayout activityLayout = (LinearLayout) view.findViewById(R.id.first_list_ll_2);
            ImageView iv1 = (ImageView) activityLayout.findViewById(R.id.first_list_iv_miao);
            ImageView iv2 = (ImageView) activityLayout.findViewById(R.id.first_list_iv_kan);
            ImageView iv3 = (ImageView) activityLayout.findViewById(R.id.first_list_iv_duo);
            ImageView iv4 = (ImageView) activityLayout.findViewById(R.id.first_list_iv_yugao);
            try {
                JSONArray activityArray = data.getJSONArray("templates").getJSONObject(1).getJSONArray("item");
                JSONObject item = activityArray.getJSONObject(0);
                String imgUrl = item.getString("imgUrl");
                x.image().bind(iv1, imgUrl);
                //保存数据
                iv1.setTag(item.getString("link"));
                //设置点击事件
                iv1.setOnClickListener(firstItem2);

                JSONObject item1 = activityArray.getJSONObject(1);
                String imgUrl1 = item1.getString("imgUrl");
                x.image().bind(iv2, imgUrl1);
                //保存数据
                iv2.setTag(item.getString("link"));
                //设置点击事件
                iv2.setOnClickListener(firstItem2);

                JSONObject item2 = activityArray.getJSONObject(3);
                String imgUrl2 = item2.getString("imgUrl");
                x.image().bind(iv3, imgUrl2);
                //保存数据
                iv3.setTag(item.getString("link"));
                //设置点击事件
                iv3.setOnClickListener(firstItem2);

                JSONObject item3 = activityArray.getJSONObject(2);
                String imgUrl3 = item3.getString("imgUrl");
                x.image().bind(iv4, imgUrl3);
                //保存数据
                iv4.setTag(item.getString("link"));
                //设置点击事件
                iv4.setOnClickListener(firstItem2);


            } catch (JSONException e) {
                e.printStackTrace();
            }
            /***********定时更新************/
            ImageView iv5 = (ImageView) view.findViewById(R.id.first_list_iv_flush);
            try {
                JSONArray activityArray = data.getJSONArray("templates").getJSONObject(2).getJSONArray("item");
                JSONObject item = activityArray.getJSONObject(0);
                String imgUrl = item.getString("imgUrl");
                x.image().bind(iv5, imgUrl);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return view;
        }

        public JSONArray getListItem() {
            JSONArray listItem = null;
            try {
                listItem = allData.getJSONArray("data");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return listItem;
        }

        @Override
        public int getCount() {
            int len = getListItem().length() + 0;
            return len + 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //加载第一条item
            if (0 == position) {
                if (convertView != null && convertView instanceof LinearLayout) {
                    return convertView;
                } else {
                    convertView = getFirstList(allData);
                    return convertView;
                }
            } else {
                //list的复用
                ImageView iv = null;
                if (convertView != null && convertView instanceof ImageView) {
                    iv = (ImageView) convertView;
                } else {
                    //返回的必须是第一层  不能返回布局里面的布局
                    iv = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.belong_list_view, null);
                }
                try {
                    JSONArray item = getListItem();
                    String imgUrl = item.getJSONObject(position - 1).getString("imgUrl");
                    x.image().bind(iv, imgUrl);
                    return iv;
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            return new TextView(getActivity());
        }
    }

    //不带ListView设置点击事件
    class FirstItemClicListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), LabelBrowserActivity.class);
            intent.putExtra("link", v.getTag() + "");
            startActivity(intent);
        }
    }

    // 带有listView的监听
    class FirstItemClicListener2 implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), GridBrowserActivity.class);
            intent.putExtra("link", v.getTag() + "");
            startActivity(intent);
        }
    }

}
